package cn.chenfeifx.core.common;

import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.scene.Node;
import javafx.scene.shape.Rectangle;

import java.util.concurrent.Callable;

public class ClipUtil {

    /**
     * 矩形裁剪
     *
     * @param node    ：裁剪区域
     * @param bindArc ：裁剪圆角
     * @return Rectangle
     */
    public static Rectangle clipRect(Node node, DoubleProperty bindArc) {
        Rectangle rectangle = new Rectangle();
        //rectangle.setSmooth(false);
        rectangle.widthProperty().bind(Bindings
                .createObjectBinding((Callable<Number>) () ->
                        node.getLayoutBounds().getWidth(), node.layoutBoundsProperty()));
        rectangle.heightProperty().bind(Bindings
                .createObjectBinding((Callable<Number>) () ->
                        node.getLayoutBounds().getHeight(), node.layoutBoundsProperty()));
        rectangle.arcWidthProperty().bind(bindArc);
        rectangle.arcHeightProperty().bind(bindArc);
        node.setClip(rectangle);
        return rectangle;
    }

}
